package com.dabai.iot.core.controller;

import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.ProductTopicConvert;
import com.dabai.iot.core.dto.ProductTopicInput;
import com.dabai.iot.core.dto.ProductTopicUpdate;
import com.dabai.iot.core.entity.ProductTopicEntity;
import com.dabai.iot.core.service.IProductTopicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息主题 控制器
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/product/topic")
public class ProductTopicController extends BaseController {

    private final IProductTopicService productTopicService;

    /**
     * 新增自定义主题
     */
    @PostMapping("/add")
    @Operation(summary = "新增自定义主题")
    public R<Void> save(@Valid @RequestBody ProductTopicInput topicInput) {
        ProductTopicEntity productTopic = ProductTopicConvert.INSTANCE.convert(topicInput);
        return toAjax(productTopicService.addTopic(productTopic));
    }

    /**
     * 修改自定义主题
     */
    @PostMapping("/update")
    @Operation(summary = "修改自定义主题")
    public R<Void> update(@Valid @RequestBody ProductTopicUpdate topicUpdate) {
        ProductTopicEntity productTopic = productTopicService.getById(topicUpdate.getId());
        if (productTopic == null) {
            return R.fail("主题不存在");
        }

        productTopic.setTopicDesc(topicUpdate.getTopicDesc());
        return toAjax(productTopicService.updateTopic(productTopic));
    }

    /**
     * 删除自定义主题
     */
    @PostMapping("/remove")
    @Operation(summary = "删除自定义主题", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        boolean result = productTopicService.removeTopic(id);
        return toAjax(result);
    }

    /**
     * 主题详情
     */
    @GetMapping("/detail")
    @Operation(summary = "主题详情", description = "传入主键id")
    public R<ProductTopicEntity> detail(@RequestParam Long id) {
        ProductTopicEntity detail = productTopicService.getById(id);
        return R.ok(detail);
    }

    /**
     * 主题列表
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "productId", description = "产品Id", required = true),
        @Parameter(name = "topicSourceType", description = "1系统主题 2自定义主题"),
        @Parameter(name = "topicDirection", description = "1设备订阅 2设备发布")})
    @Operation(summary = "主题列表", description = "传入查询参数")
    public R<List<ProductTopicEntity>> list(@RequestParam Long productId, String topicSourceType, String topicDirection) {
        List<ProductTopicEntity> list = productTopicService.selectList(productId, topicSourceType, topicDirection);
        return R.ok(list);
    }

    /**
     * 主题分页
     */
    @GetMapping("/page")
    @Parameters({
        @Parameter(name = "productId", description = "产品Id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "topicSourceType", description = "Topic 所属类别", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "topicDirection", description = "Topic 通信方向", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
    })
    @Operation(summary = "主题分页", description = "传入查询参数")
    public TableDataInfo<ProductTopicEntity> page(PageQuery pageQuery, @RequestParam Long productId, String topicSourceType, String topicDirection) {
        return productTopicService.selectPage(pageQuery.build(), productId, topicSourceType, topicDirection);
    }

}
